package cn.blnp.net.flowable.boot.custom.event;

import cn.blnp.net.flowable.boot.customer.flowable.event.CustomFlowableEventType;
import cn.blnp.net.flowable.boot.customer.flowable.event.service.impl.FlowableTaskUrgingEventImpl;
import lombok.extern.slf4j.Slf4j;
import org.flowable.bpmn.model.BpmnModel;
import org.flowable.common.engine.api.delegate.event.FlowableEngineEventType;
import org.flowable.common.engine.impl.event.FlowableEventSupport;
import org.flowable.engine.IdentityService;
import org.flowable.engine.RepositoryService;
import org.flowable.engine.RuntimeService;
import org.flowable.engine.TaskService;
import org.flowable.engine.repository.Deployment;
import org.flowable.engine.repository.ProcessDefinition;
import org.flowable.engine.runtime.ProcessInstance;
import org.flowable.task.api.Task;
import org.flowable.task.api.TaskQuery;
import org.flowable.task.service.impl.persistence.entity.TaskEntity;
import org.junit.jupiter.api.Test;
import org.springframework.boot.test.context.SpringBootTest;

import javax.annotation.Resource;

/**
 * <h3>自定义流程事件实现</h3>
 *
 * @author <a href="mailto:blnp.yibin@qq.com">lyb</a>
 * @version 1.0
 * @since 2025/6/6 16:17
 */
@Slf4j
@SpringBootTest
public class CusFlowEventTest {

    @Resource
    private RepositoryService repositoryService;
    @Resource
    private RuntimeService runtimeService;
    @Resource
    private IdentityService identityService;
    @Resource
    private TaskService taskService;

    /**
     *  <h2>自定义催办事件案例演示</h2>
     *  <p>
     *      该流程为运维申请流程，这里将由“专家服务”用户任务触发自定义的催办事件.
     *      <ul>
     *          <li><b>流程定义文件：</b>{@link flow/customEventProcess.bpmn20.xml}</li>
     *          <li>
     *              <b>流程预览图：</b></br>
     *              <img src="https://cdn4.winhlb.com/2025/06/06/68429d838e595.png" >
     *          </li>
     *      </ul>
     *  </p>
     **/
    @Test
    public void test() {
        Deployment deploy = repositoryService.createDeployment()
                .addClasspathResource("flow/customActivityBehaviorProcess.bpmn20.xml")
                .name("自定义催办事件案例")
                .deploy();
        //流程定义信息
        ProcessDefinition processDefinition = repositoryService.createProcessDefinitionQuery()
                .deploymentId(deploy.getId())
                .singleResult();
        log.info("流程定义ID：{}", processDefinition.getId());

        //启动流程实例
        ProcessInstance procInst = runtimeService.startProcessInstanceById(processDefinition.getId());
        TaskQuery taskQuery = taskService.createTaskQuery()
                .processInstanceId(procInst.getId());
        //查询并完成第一个用户任务
        Task firstTask = taskQuery.singleResult();
        taskService.complete(firstTask.getId());
        //查询第二个用户任务
        Task secondTask = taskQuery.singleResult();
        /**
         *  需要注意，如果自定义的事件监听器没有注册到流程引擎事件分发器中是不生效的
         **/
        //触发自定义事件
        runtimeService.dispatchEvent(
                new FlowableTaskUrgingEventImpl(
                        (TaskEntity) secondTask,
                        FlowableEngineEventType.CUSTOM,
                        CustomFlowableEventType.TASK_URGING
                )
        );

        /**
         *  演示结果：
         *  2025-06-09 11:09:04.509  INFO 10588 --- [           main] c.b.n.f.b.custom.event.CusFlowEventTest  : 流程定义ID：customActivityBehaviorProcess:12:50004
         * 2025-06-09 11:09:04.593  INFO 10588 --- [           main] f.b.c.f.b.CustomUserTaskActivityBehavior : TaskId为50013的用户任务只有一个候选人，将设置为办理人
         * 2025-06-09 11:09:09.930  INFO 10588 --- [           main] .b.n.f.b.c.f.e.l.TaskUrgingEventListener : 专家服务被催办了！
         **/
    }
}
